//
//  RemoteButtons.m
//  touchpadpro
//
//  Created by Jahanzeb Sherwani on 03/20/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "RemoteButtons.h"

@implementation RemoteButton

- (id)initWithPage:(int)Page Row:(int)Row Col:(int)Col ImageType:(int)ImageType Title:(NSString*)Title KeyAlias:(NSString*)KeyAlias KeyReleaseAlias:(NSString*)KeyReleaseAlias
{
	page = Page;
	row = Row;
	col = Col;
	imageType = ImageType;
	title = Title;
	keyAlias = KeyAlias;
	keyReleaseAlias = KeyReleaseAlias;
	return self;
}
- (id)initWithPage:(int)Page Row:(int)Row Col:(int)Col ImageType:(int)ImageType Title:(NSString*)Title KeyAlias:(NSString*)KeyAlias;
{
	[self initWithPage:(int)Page Row:(int)Row Col:(int)Col ImageType:(int)ImageType Title:(NSString*)Title KeyAlias:(NSString*)KeyAlias KeyReleaseAlias:nil];
}

- (void)setPage:(int)Page Row:(int)Row Col:(int)Col
{
	page = Page;
	row = Row;
	col = Col;
}

- (void)setKeyAlias:(NSString *)KeyAlias
{
	keyAlias = KeyAlias;
}

- (void)setTitle:(NSString*) Title
{
	title = Title;
}

- (void)setControl:(id)Control
{
	control = Control;
}

- (id)getControl
{
	return control;
}

- (NSString *)getKeyAlias
{
	return keyAlias;
}

- (NSString *)getKeyReleaseAlias
{
	return keyReleaseAlias;
}

- (NSString *)getTitle
{
	if ([title isEqualToString: @""] && imageType != kSpecial)
		return [keyAlias lowercaseString];
	else
		return title;
}
- (int)getPage
{
	return page;
}
- (int)getRow
{
	return row;
}
- (int)getCol
{
	return col;
}

- (int)getImageType
{
	return imageType;
}

- (NSString *)getImage
{
	if (imageType == kWhite)
		return @"remote/whitebg.png";
	if (imageType == kBlack)
		return @"remote/blackbg.png";
	if (imageType == kSpecial)
		return [NSString stringWithFormat: @"remote/%@.png", [title isEqualToString: @""] ? [keyAlias lowercaseString] : [title lowercaseString]];
}

@end

@implementation RemoteButtons

- (id)initWithMediaConnection: (bool)isConnected
{
	isMediaConnected = isConnected;
	
	buttonsArray = [[NSMutableArray alloc] init];

	[self initButtons];

	return self;
}

- (void)dealloc
{
	[buttonsArray dealloc];
    [super dealloc];
}

- (void)addButton: (RemoteButton*) button
{
	[buttonsArray addObject: button];
	if (totalPages < [button getPage])
		totalPages = [button getPage];
	if (totalRowsInPage[[button getPage]] < [button getRow])
		totalRowsInPage[[button getPage]] = [button getRow];
	if (totalColsInPage[[button getPage]] < [button getCol])
		totalColsInPage[[button getPage]] = [button getCol];
//	NSLog(@"max %d %d %d", maxpage, maxrow, maxcol);
}

- (NSArray *)getButtonsArray
{
	return buttonsArray;
}

- (RemoteButton*)getButtonByControl:(id)control
{
	int i;
	for (i=0; i<[buttonsArray count]; i++)
		if ([(RemoteButton*)[buttonsArray objectAtIndex: i] getControl] == control)
			return (RemoteButton*)[buttonsArray objectAtIndex: i];
	return nil;
}

- (RemoteButton*)getButtonByTitle:(NSString *)title
{
	int i;
	for (i=0; i<[buttonsArray count]; i++)
		if ([[(RemoteButton*)[buttonsArray objectAtIndex: i] getTitle] isEqualToString: title])
			return (RemoteButton*)[buttonsArray objectAtIndex: i];
	return nil;
}

- (RemoteButton*)getButtonByPage:(int)page Row:(int)row Col:(int)col
{
	int i;
	for (i=0; i<[buttonsArray count]; i++)
	{
		RemoteButton *button = (RemoteButton*)[buttonsArray objectAtIndex: i];
		if ([button getPage] == page && [button getRow] == row && [button getCol] == col)
			return (RemoteButton*)[buttonsArray objectAtIndex: i];
	}
	return nil;
}

- (int) getTotalPages
{
	return totalPages + 1;
}

- (int) getTotalRowsInPage:(int)Page
{
	return totalRowsInPage[Page] + 1;
}

- (int) getTotalColsInPage:(int)Page;
{
	return totalColsInPage[Page] + 1;
}

- (void) initButtons
{
	int page, row, col;
	page=row=col=0;
//////////// first page
	if (isMediaConnected)
	{
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"Play" KeyAlias:@"TunesPlay"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"Rewind" KeyAlias:@"TunesRewind"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"FastForward" KeyAlias:@"TunesFastForward"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"Stop" KeyAlias:@"TunesStop"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"PreviousTrack" KeyAlias:@"TunesPreviousTrack"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"NextTrack" KeyAlias:@"TunesNextTrack"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"Mute" KeyAlias:@"TunesMute"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"VolumeDown" KeyAlias:@"TunesVolumeDown"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"VolumeUp" KeyAlias:@"TunesVolumeUp"]];
	row=col=0; page++;
	}
//////////// second page
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Esc"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"" KeyAlias:@"Up"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Back"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"" KeyAlias:@"Left"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"" KeyAlias:@"OK"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"" KeyAlias:@"Right"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Tab"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kSpecial Title:@"" KeyAlias:@"Down"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Space"]];
	row=col=0; page++;
//////////// third page
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F1" KeyAlias:@"F1"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F2" KeyAlias:@"F2"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F3" KeyAlias:@"F3"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F4" KeyAlias:@"F4"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F5" KeyAlias:@"F5"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F6" KeyAlias:@"F6"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F7" KeyAlias:@"F7"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F8" KeyAlias:@"F8"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F9" KeyAlias:@"F9"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F10" KeyAlias:@"F10"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F11" KeyAlias:@"F11"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"F12" KeyAlias:@"F12"]];
	row=col=0; page++;
//////////// fourth page
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Del"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Tab"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"Space"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"/"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"7"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"8"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"9"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"*"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"4"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"5"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"6"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"-"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"1"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"2"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"3"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"+"]];
	col=0; row++;
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"0"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"" KeyAlias:@"."]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"c" KeyAlias:@"Back"]];
	[self addButton: [[RemoteButton alloc] initWithPage:page Row:row Col:col++ ImageType:kWhite Title:@"enter" KeyAlias:@"OK"]];
	row=col=0; page++;
}

@end
